*************************************
*** Preliminary steps

/* In order to successfully replicate the results, you need to install the 
following packages: 

st0085_2.pkg        from:  http://www.stata-journal.com/software/sj14-2/

reghdfe             from http://fmwww.bc.edu/RePEc/bocode/r

outreg2.pkg         from:  http://fmwww.bc.edu/RePEc/bocode/o/

In addition, ensure you have ftools (required by reghdfe), which can be installed with

ssc install ftools

*/

/* The code assumes you have set the working directory to the folder that 
contains the datasets. All outputs are save to that same folder. 
*/


***********************************
* Figure 1 
***********************************

use CDS_DMITD_replication.dta, clear


 
xtset countryname_code  date2

gen deltawindexinrow=windexinrow-l.windexinrow

gen pos = 1 if l.deltawindexinrow>=0 & MV>6

recode pos(.=0) if MV>6

sort  date2 
by   date2: egen pos_day = mean(pos)
collapse (mean) pos_day, by(month_code)

tsset month_code
tsline pos_day, ytitle("Fraction of Country-Days with {&Delta}{subscript:it}{&ge}0") xtitle("Month") xlabel(1 "2020" 13 "2021" 25 "2022") ///
scheme(s2mono) graphregion(fcolor(white)) 

graph export "Figure1.eps", as(eps) name("Graph") preview(off) replace

graph export "Figure1pdf.pdf", as(pdf) name("Graph") replace

***********************************
* PAPER MODELS
***********************************

use CDS_DMITD_replication.dta, clear

xtset countryname_code date2


gen deltawindexinrow=windexinrow-l.windexinrow

***********************************
* TABLE 1 
***********************************



gen lstringencyindex = l.stringencyindex
gen lwindexinrow = l.windexinrow 
gen lwtraderow = l.wtraderow
gen lwnationrow = l.wnationrow 
gen lwindexideorow =l.windexideorow
gen lnew_cases_per_million2 = l.new_cases_per_million2
gen lnew_deaths_per_million2 = l.new_deaths_per_million2

label variable lstringencyindex "Stringency (lag)"
label variable lwindexinrow "Geography (lag)"
label variable lwtraderow "Trade (lag)"
label variable lwnationrow "Nationalism (lag)"
label variable lwindexideorow "Left-Right (lag)"
label variable lnew_cases_per_million2 "New Cases (lag)"
label variable lnew_deaths_per_million2 "New Deaths (lag)"


** Notice: MV>6 makes the analysis start with January 7 2020.

************************************************************************************
******** TABLE 1 Baseline models of policy diffusion outreg
************************************************************************************

reghdfe stringencyindex lstringencyindex lwindexinrow lnew_cases_per_million2 ///
lnew_deaths_per_million2 if MV>6,    abs(countryname_code month_code) vce(r)  

outreg2 est1 using table1.tex, replace keep(lwindexinrow  lwtraderow lwindexideorow lwnationrow lstringencyindex lnew_cases_per_million2 lnew_deaths_per_million2)  alpha(0.001, 0.01, 0.05)nocons   word label dec(3)     tex addtext(Country FE, YES, Month FE, YES)  

reghdfe stringencyindex lstringencyindex lwtraderow lnew_cases_per_million2 ///
lnew_deaths_per_million2 if MV>6,    abs(countryname_code month_code) vce(r)  

outreg2  est2 using table1.tex, keep(lwindexinrow  lwtraderow lwindexideorow lwnationrow lstringencyindex lnew_cases_per_million2 lnew_deaths_per_million2)  alpha(0.001, 0.01, 0.05)nocons   word label dec(3)    tex addtext(Country FE, YES, Month FE, YES)  

reghdfe stringencyindex lstringencyindex lwindexideorow lnew_cases_per_million2 ///
lnew_deaths_per_million2 if MV>6,    abs(countryname_code month_code) vce(r)  

outreg2  est2 using table1.tex, keep(lwindexinrow  lwtraderow lwindexideorow lwnationrow lstringencyindex lnew_cases_per_million2 lnew_deaths_per_million2)  alpha(0.001, 0.01, 0.05) nocons   word label dec(3)      tex addtext(Country FE, YES, Month FE, YES)  

reghdfe stringencyindex lstringencyindex lwnationrow lnew_cases_per_million2 ///
lnew_deaths_per_million2 if MV>6,    abs(countryname_code month_code) vce(r)  

outreg2  est2 using table1.tex, keep(lwindexinrow  lwtraderow lwindexideorow lwnationrow lstringencyindex lnew_cases_per_million2 lnew_deaths_per_million2)  alpha(0.001, 0.01, 0.05) nocons   word label dec(3)      tex addtext(Country FE, YES, Month FE, YES)  

reghdfe stringencyindex lstringencyindex   lwindexinrow  lwtraderow lwnationrow  lnew_cases_per_million2 ///
lnew_deaths_per_million2 if MV>6,    abs(countryname_code month_code) vce(r)  

outreg2  est2 using table1.tex, keep(lwindexinrow  lwtraderow lwindexideorow lwnationrow lstringencyindex lnew_cases_per_million2 lnew_deaths_per_million2)  alpha(0.001, 0.01, 0.05) nocons   word label dec(3)      tex addtext(Country FE, YES, Month FE, YES)  

************************************************************************************
******** TABLE 2 Models accounting for epidemic trajectories
************************************************************************************

cap drop with_d
cap drop with_s


***** here preparation of complicated metrics

* first and second derivative are estimated by the 7-day difference in new deaths per million (rescaled so 
* we avoid those significant coefs with only 4 zeros after rounding

 gen first_derivative = (new_deaths_per_million2-l7.new_deaths_per_million2)/10
 gen second_derivative = (first_derivative-l7.first_derivative)/10

* first and second derivative are estimated by the 7-day difference in new cases per million
 gen cases_first_derivative = (new_cases_per_million2-l7.new_cases_per_million2)/10
 gen cases_second_derivative = (cases_first_derivative-l7.cases_first_derivative)/10

** a dummy for "exponential growth" of deaths, where new deaths are increasing at an increasing rate
 gen exponential_phase=first>0&second>0 &first!=.&second!=.
 replace exponential_phas=. if first==.|cases_second==.
 gen cases_exponential_phase = cases_first>0 &cases_second>0 
 replace cases_exponential_phas=. if cases_first==.|cases_second==.

*** dummies for the sign of the derivatives 
 gen sign_first=first>0 
 replace sign_f =. if first==.
 gen sign_second=second>0 
 replace sign_s =. if second==.
 gen cases_sign_first=cases_first>0 
 replace cases_sign_first=. if cases_first==.
 gen cases_sign_second=cases_second>0  
 replace cases_sign_second=. if cases_second==.


gen lfirst = l.first
gen lsecond = l.second 
gen lsign_f = l.sign_f 
gen lsign_s = l.sign_s
gen lcases_fir = l.cases_fir 
gen lcases_sec = l.cases_sec
gen lcases_sign_f = l.cases_sign_f 
gen lcases_sign_s = l.cases_sign_s
gen lexponential = l.exponential
gen inter_exp =l.exponential*l.new_deaths_per_million2 
gen inter_cas =l.exponential*l.new_cases_per_million2 
gen lcases_exponential = l.cases_exponential
gen inter_cas2 = l.cases_exponential*l.new_cases_per_million2  
gen inter_exp2 = l.cases_exponential*l.new_deaths_per_million2


************************************************************************************
******** TABLE 2 Models accounting for epidemic trajectories outreg
************************************************************************************

* column 1
reghdfe stringencyindex lstringencyindex lwindexinrow lnew_cases_per_million2  lnew_deaths_per_million2  lfirst lsecond if MV>6,    abs(countryname_code month_code) vce(r) 

outreg2 est1 using table2.tex, replace keep(lstringencyindex lwindexinrow  lnew_cases_per_million2 lnew_deaths_per_million2 lfirst lsecond  lsign_f lsign_s  lfirst lsecond lcases_fir lcases_sec lsign_f lsign_s lcases_sign_f lcases_sign_s lexponential inter_exp lcases_exponential inter_cas2 inter_cas inter_exp2 )  alpha(0.001, 0.01, 0.05) nocons   word label dec(3)     tex addtext(Country FE, YES, Month FE, YES)  

* column 2
reghdfe stringencyindex lstringencyindex lwindexinrow lnew_cases_per_million2  lnew_deaths_per_million2  lsign_f lsign_s if MV>6,    abs(countryname_code month_code) vce(r) 

outreg2 est1 using table2.tex,  keep(lstringencyindex lwindexinrow  lnew_cases_per_million2 lnew_deaths_per_million2 lfirst lsecond  lsign_f lsign_s  lfirst lsecond lcases_fir lcases_sec lsign_f lsign_s lcases_sign_f lcases_sign_s lexponential inter_exp lcases_exponential inter_cas2 inter_cas inter_exp2 )  alpha(0.001, 0.01, 0.05) nocons   word label dec(3)     tex addtext(Country FE, YES, Month FE, YES)  

gen with_sign=e(sample)
** with first and second derivative of cases too

* column 3
reghdfe stringencyindex lstringencyindex lwindexinrow lnew_cases_per_million2  lnew_deaths_per_million2 lfirst lsecond lcases_fir lcases_sec if MV>6,    abs(countryname_code month_code) vce(r) 

outreg2 est1 using table2.tex,  keep(lstringencyindex lwindexinrow  lnew_cases_per_million2 lnew_deaths_per_million2 lfirst lsecond  lsign_f lsign_s  lfirst lsecond lcases_fir lcases_sec lsign_f lsign_s lcases_sign_f lcases_sign_s lexponential inter_exp lcases_exponential inter_cas2 inter_cas inter_exp2 )  alpha(0.001, 0.01, 0.05) nocons   word label dec(3)     tex addtext(Country FE, YES, Month FE, YES)  

** with sign of first and second derivative of cases too

* column 4
reghdfe stringencyindex lstringencyindex lwindexinrow lnew_cases_per_million2  lnew_deaths_per_million2 lsign_f lsign_s lcases_sign_f lcases_sign_s if MV>6,    abs(countryname_code month_code) vce(r) 

outreg2 est1 using table2.tex,  keep(lstringencyindex lwindexinrow  lnew_cases_per_million2 lnew_deaths_per_million2 lfirst lsecond  lsign_f lsign_s  lfirst lsecond lcases_fir lcases_sec lsign_f lsign_s lcases_sign_f lcases_sign_s lexponential inter_exp lcases_exponential inter_cas2 inter_cas inter_exp2 )  alpha(0.001, 0.01, 0.05) nocons   word label dec(3)     tex addtext(Country FE, YES, Month FE, YES)  

** with exponential (deaths) dummy interacted with new deaths

* column 5
reghdfe stringencyindex lstringencyindex lwindexinrow lnew_cases_per_million2 lexponential lnew_deaths_per_million2 inter_exp  if MV>6,    abs(countryname_code month_code) vce(r) 

outreg2 est1 using table2.tex,  keep(lstringencyindex lwindexinrow  lnew_cases_per_million2 lnew_deaths_per_million2 lfirst lsecond  lsign_f lsign_s  lfirst lsecond lcases_fir lcases_sec lsign_f lsign_s lcases_sign_f lcases_sign_s lexponential inter_exp lcases_exponential inter_cas2 inter_cas inter_exp2 )  alpha(0.001, 0.01, 0.05) nocons   word label dec(3)     tex addtext(Country FE, YES, Month FE, YES)  

** with exponential deaths and exponential cases interacted with new deaths and new cases 

* column 6
reghdfe stringencyindex lstringencyindex lwindexinrow   lnew_deaths_per_million2 lexponential lnew_deaths_per_million2 inter_exp lcases_exponential inter_cas inter_exp2 inter_cas2  if MV>6,    abs(countryname_code month_code) vce(r) 

outreg2 est1 using table2.tex,  keep(lstringencyindex lwindexinrow  lnew_cases_per_million2 lnew_deaths_per_million2 lfirst lsecond  lsign_f lsign_s  lfirst lsecond lcases_fir lcases_sec lsign_f lsign_s lcases_sign_f lcases_sign_s lexponential inter_exp lcases_exponential inter_cas2 inter_cas inter_exp2 )  alpha(0.001, 0.01, 0.05) nocons   word label dec(3)     tex addtext(Country FE, YES, Month FE, YES)  


************************************************************************************
******** TABLE 3 Split-sample models based on the sign of delta
************************************************************************************

reghdfe stringencyindex lstringencyindex lwindexinrow lnew_cases_per_million2 ///
lnew_deaths_per_million2  if l.deltawindexinrow>=0  & MV>6,    abs(countryname_code month_code) vce(r)  

outreg2 est1 using table3.tex, replace keep(lwindexinrow  lstringencyindex lnew_cases_per_million2 lnew_deaths_per_million2) alpha(0.001, 0.01, 0.05) nocons   word label dec(3)     tex addtext(Delta, >=0, Country FE, YES, Month FE, YES)  

reghdfe stringencyindex lstringencyindex lwindexinrow lnew_cases_per_million2 ///
lnew_deaths_per_million2  if l.deltawindexinrow<0  & MV>6,    abs(countryname_code month_code) vce(r)  

outreg2 est1 using table3.tex,  keep(lwindexinrow  lstringencyindex lnew_cases_per_million2 lnew_deaths_per_million2) alpha(0.001, 0.01, 0.05) nocons   word label dec(3)     tex addtext(Delta, <0, Country FE, YES, Month FE, YES)  


************************************************************************************
******** TABLE 4 government effectiveness
************************************************************************************

* World Bank variables: Voice and Accountability (vae), Political Stability and Absence of Violence/Terrorism (pve), Government Effectiveness (gee), Regulatory Quality (rqe), Rule of Law (rle), and Control of Corruption (cce)


** merge to healthcare (and SPI used later)

drop _merge 

merge m:1 iso_code using "merged_healthcare_dataset.dta"

xtset countryname_code date2

gen inter = lwindexinrow*gee
gen inter2 = lnew_deaths_per_million2*gee

label variable inter "W Geography (lag) * Government Effectiveness"
label variable inter2 "New Deaths (lag) * Government Effectiveness"


************************************************************************************
******** TABLE 4 outreg  government effectiveness
************************************************************************************

reghdfe stringencyindex  lstringencyindex lwindexinrow inter  lnew_deaths_per_million2 inter2    lnew_cases_per_million2 ///
 if  MV>6 ,     abs(countryname_code month_code) vce(r)

 outreg2 est1 using table4.tex, replace keep(lstringencyindex lwindexinrow inter  lnew_deaths_per_million2 inter2    lnew_cases_per_million2) alpha(0.001, 0.01, 0.05)  nocons   word label dec(3)     tex addtext(Delta, All, Country FE, YES, Month FE, YES)  
 
reghdfe stringencyindex  lstringencyindex lwindexinrow inter  lnew_deaths_per_million2 inter2    lnew_cases_per_million2 ///
 if l.deltawindexinrow>=0  & MV>6 ,     abs(countryname_code month_code) vce(r)

 outreg2 est1 using table4.tex,  keep(lstringencyindex lwindexinrow inter  lnew_deaths_per_million2 inter2    lnew_cases_per_million2) alpha(0.001, 0.01, 0.05)  nocons   word label dec(3)     tex addtext(Delta, >=0, Country FE, YES, Month FE, YES)  
 
reghdfe stringencyindex  lstringencyindex lwindexinrow inter  lnew_deaths_per_million2 inter2    lnew_cases_per_million2 ///
 if l.deltawindexinrow<0  & MV>6 ,     abs(countryname_code month_code) vce(r)

  outreg2 est1 using table4.tex,  keep(lstringencyindex lwindexinrow inter  lnew_deaths_per_million2 inter2    lnew_cases_per_million2) alpha(0.001, 0.01, 0.05)  nocons   word label dec(3)     tex addtext(Delta, <0, Country FE, YES, Month FE, YES)
  
************************************************************************************
******** TABLE 4 
************************************************************************************



reghdfe stringencyindex l.stringencyindex  l.new_cases_per_million2 ///
l.c.new_deaths_per_million2##c.hospital_beds_p1k l.c.windexinrow##c.hospital_beds_p1k if MV>6,    abs(countryname_code month_code) vce(r)  

outreg2 using table4.tex,  replace  keep( L.stringencyindex L.new_cases_per_million2 L.new_deaths_per_million2  c.l.windexinrow##c.hospital_beds_p1k c.l.new_deaths_per_million2##c.hospital_beds_p1k) alpha(0.001, 0.01, 0.05)  nocons word label dec(3)   tex addtext(DELTA, ALL, Controls, YES, Country FE, YES, Month FE, YES)  

reghdfe stringencyindex l.stringencyindex  l.new_cases_per_million2 ///
l.c.new_deaths_per_million2##c.hospital_beds_p1k l.c.windexinrow##c.hospital_beds_p1k if l.deltawindexinrow>=0 & MV>6,    abs(countryname_code month_code) vce(r) 

outreg2 using table4.tex,    keep( L.stringencyindex L.new_cases_per_million2 L.new_deaths_per_million2  c.l.windexinrow##c.hospital_beds_p1k c.l.new_deaths_per_million2##c.hospital_beds_p1k) alpha(0.001, 0.01, 0.05)  nocons word label dec(3)   tex addtext(DELTA, POSITIVE, Controls, YES, Country FE, YES, Month FE, YES)  
 
reghdfe stringencyindex l.stringencyindex  l.new_cases_per_million2 ///
l.c.new_deaths_per_million2##c.hospital_beds_p1k l.c.windexinrow##c.hospital_beds_p1k if l.deltawindexinrow<0 & MV>6,    abs(countryname_code month_code) vce(r)  

outreg2 using table4.tex,    keep( L.stringencyindex L.new_cases_per_million2 L.new_deaths_per_million2  c.l.windexinrow##c.hospital_beds_p1k c.l.new_deaths_per_million2##c.hospital_beds_p1k) alpha(0.001, 0.01, 0.05)  nocons word label dec(3)   tex addtext(DELTA, NEGATIVE, Controls, YES, Country FE, YES, Month FE, YES)  
 


************************************************************************************
******** TABLE 4 all together Models interacting institutional quality and the spatial 
************************************************************************************

drop inter inter2
gen inter = lwindexinrow*gee
gen inter2 = lnew_deaths_per_million2*gee

label variable inter "W Geography (lag) * Institutions"
label variable inter2 "New Deaths (lag) * Institutions"

reghdfe stringencyindex  lstringencyindex lwindexinrow inter  lnew_deaths_per_million2 inter2    lnew_cases_per_million2 ///
 if  MV>6 ,     abs(countryname_code month_code) vce(r)

 outreg2 est1 using table4.tex, replace keep(lstringencyindex lwindexinrow inter  lnew_deaths_per_million2 inter2    lnew_cases_per_million2) alpha(0.001, 0.01, 0.05)  nocons   word label dec(3)     tex addtext(Delta, All, Country FE, YES, Month FE, YES)  
 
reghdfe stringencyindex  lstringencyindex lwindexinrow inter  lnew_deaths_per_million2 inter2    lnew_cases_per_million2 ///
 if l.deltawindexinrow>=0  & MV>6 ,     abs(countryname_code month_code) vce(r)

 outreg2 est1 using table4.tex,  keep(lstringencyindex lwindexinrow inter  lnew_deaths_per_million2 inter2    lnew_cases_per_million2) alpha(0.001, 0.01, 0.05)  nocons   word label dec(3)     tex addtext(Delta, >=0, Country FE, YES, Month FE, YES)  
 
reghdfe stringencyindex  lstringencyindex lwindexinrow inter  lnew_deaths_per_million2 inter2    lnew_cases_per_million2 ///
 if l.deltawindexinrow<0  & MV>6 ,     abs(countryname_code month_code) vce(r)

   outreg2 est1 using table4.tex,  keep(lstringencyindex lwindexinrow inter  lnew_deaths_per_million2 inter2    lnew_cases_per_million2) alpha(0.001, 0.01, 0.05)  nocons   word label dec(3)     tex addtext(Delta, <0, Country FE, YES, Month FE, YES)
   
drop inter inter2
gen inter = lwindexinrow*hospital_beds_p1k
gen inter2 = lnew_deaths_per_million2*hospital_beds_p1k

label variable inter "W Geography (lag) * Institutions"
label variable inter2 "New Deaths (lag) * Institutions"

reghdfe stringencyindex  lstringencyindex lwindexinrow inter  lnew_deaths_per_million2 inter2    lnew_cases_per_million2 ///
 if  MV>6 ,     abs(countryname_code month_code) vce(r)

 outreg2 est1 using table4.tex,  keep(lstringencyindex lwindexinrow inter  lnew_deaths_per_million2 inter2    lnew_cases_per_million2) alpha(0.001, 0.01, 0.05)  nocons   word label dec(3)     tex addtext(Delta, All, Country FE, YES, Month FE, YES)  
 
reghdfe stringencyindex  lstringencyindex lwindexinrow inter  lnew_deaths_per_million2 inter2    lnew_cases_per_million2 ///
 if l.deltawindexinrow>=0  & MV>6 ,     abs(countryname_code month_code) vce(r)

 outreg2 est1 using table4.tex,  keep(lstringencyindex lwindexinrow inter  lnew_deaths_per_million2 inter2    lnew_cases_per_million2) alpha(0.001, 0.01, 0.05)  nocons   word label dec(3)     tex addtext(Delta, >=0, Country FE, YES, Month FE, YES)  
 
reghdfe stringencyindex  lstringencyindex lwindexinrow inter  lnew_deaths_per_million2 inter2    lnew_cases_per_million2 ///
 if l.deltawindexinrow<0  & MV>6 ,     abs(countryname_code month_code) vce(r)

   outreg2 est1 using table4.tex,  keep(lstringencyindex lwindexinrow inter  lnew_deaths_per_million2 inter2    lnew_cases_per_million2) alpha(0.001, 0.01, 0.05)  nocons   word label dec(3)     tex addtext(Delta, <0, Country FE, YES, Month FE, YES)
   

*************************
*************************
* Interaction IQ
*************************
*************************

use CDS_DMITD_replication.dta, replace

xtset countryname_code date2


gen deltawindexinrow=windexinrow-l.windexinrow

reghdfe stringencyindex c.l.new_deaths_per_million2##c.gee l.stringencyindex c.l.windexinrow##c.gee l.new_cases_per_million2 ///
 if l.deltawindexinrow<0  & MV>6 ,     abs(countryname_code month_code) vce(r)

matrix b=e(b)
matrix V=e(V)
matlist b
matlist V
scalar b1=b[1,1]
scalar b3=b[1,3]
scalar list b1 b3
scalar varb1=V[1,1]
scalar varb3=V[3,3]
scalar covb1b3=V[1,3]
scalar list b1 b3 varb1 varb3 covb1b3

sum gee if e(sample)
generate MVZ=((_n/10)-.4)
sum MVZ
replace MVZ=. if MVZ>2
sum MVZ

gen conbx=b1+b3*MVZ if MVZ!=.
gen consx=sqrt(varb1+varb3*(MVZ^2)+2*covb1b3*MVZ) if MVZ!=.
gen ax=1.96*consx
gen upperx=conbx+ax
gen lowerx=conbx-ax

gen yline=0

graph twoway hist gee, width(0.1) percent color(gs14) yaxis(2) ///
|| line conbx MVZ, clpattern(solid) clwidth(medium) clcolor(black) yaxis(1) ///
|| line upperx MVZ, clpattern(dash) clwidth(thin) clcolor(black) ///
|| line lowerx MVZ, clpattern(dash) clwidth(thin) clcolor(black) ///
|| line yline MVZ, clwidth(thin) clcolor(black) clpattern(solid) ///
|| , ///
xlabel(-.3 (.1) 2, nogrid labsize(2)) ///
ylabel(-.06 (.02) .1, axis(1) nogrid labsize(2)) ///
ylabel(, axis(2) nogrid labsize(2)) ///
yscale(noline alt) ///
yscale(noline alt axis(2)) ///
xscale(noline) ///
legend(off) ///
xtitle("Institutional Quality" , size(2.5) ) ///
ytitle("Marginal Effect of Lagged Deaths" , axis(1) size(2.5)) ///
ytitle("Percentage of Observations" , axis(2) size(2.5)) ///
xsca(titlegap(2)) ///
ysca(titlegap(2)) ///
scheme(s2mono) graphregion(fcolor(white) ilcolor(white) lcolor(white)) note("Model: only with {&Delta}<0")  saving(a.gph, replace)
 
 
graph export "Figure2Right.eps", as(eps) name("Graph") preview(off) replace

graph export "Figure2Rightpdf.pdf", as(pdf) name("Graph") replace
 
 
reghdfe stringencyindex c.l.windexinrow##c.gee l.stringencyindex  l.new_cases_per_million2 ///
 c.l.new_deaths_per_million2##c.gee if l.deltawindexinrow>=0  & MV>6,     abs(countryname_code month_code) vce(r)
 
matrix b=e(b)
matrix V=e(V)
matlist b
matlist V
scalar b1=b[1,1]
scalar b3=b[1,3]
scalar list b1 b3
scalar varb1=V[1,1]
scalar varb3=V[3,3]
scalar covb1b3=V[1,3]
scalar list b1 b3 varb1 varb3 covb1b3

cap drop conbx consx ax upperx lowerx 

gen conbx=b1+b3*MVZ if MVZ!=.
gen consx=sqrt(varb1+varb3*(MVZ^2)+2*covb1b3*MVZ) if MVZ!=.
gen ax=1.96*consx
gen upperx=conbx+ax
gen lowerx=conbx-ax

graph twoway hist gee, width(0.1) percent color(gs14) yaxis(2) ////
|| line conbx MVZ, clpattern(solid) clwidth(medium) clcolor(black) yaxis(1) ///
|| line upperx MVZ, clpattern(dash) clwidth(thin) clcolor(black) ///
|| line lowerx MVZ, clpattern(dash) clwidth(thin) clcolor(black)  , ///
xlabel(-.3 (.1) 2, nogrid labsize(2)) ///
ylabel(.01 (.01) .04, axis(1) nogrid labsize(2)) ///
ylabel(, axis(2) nogrid labsize(2)) ///
yscale(range(.01 .04) axis(1)) ///
yscale(noline alt) ///
yscale(noline alt axis(2)) ///
xscale(noline) ///
legend(off) ///
xtitle("Institutional Quality" , size(2.5) ) ///
ytitle("Marginal Effect of W - Geographical Proximity" , axis(1) size(2.5)) ///
ytitle("Percentage of Observations" , axis(2) size(2.5)) ///
xsca(titlegap(2)) ///
ysca(titlegap(2)) ///
scheme(s2mono) graphregion(fcolor(white) ilcolor(white) lcolor(white)) note("Model: only with {&Delta}>=0") saving(b.gph, replace)


**** Create Figure 2

gr combine b.gph a.gph, scheme(s2mono) graphregion(fcolor(white) ilcolor(white) lcolor(white))



graph export "Figure2.eps", as(eps) name("Graph") preview(off) replace

graph export "Figure2pdf.pdf", as(pdf) name("Graph") replace
 
*************************
*************************
* Interaction hospital
*************************
*************************

use CDS_DMITD_replication.dta, replace

xtset countryname_code date2


gen deltawindexinrow=windexinrow-l.windexinrow

cap drop _merge

merge m:1 iso_code using "merged_healthcare_dataset.dta"


xtset countryname_code date2


reghdfe stringencyindex  c.l.new_deaths_per_million2##c.hospital_beds_p1k l.stringencyindex  l.new_cases_per_million2 ///
 c.l.windexinrow##c.hospital_beds_p1k if l.deltawindexinrow<0  & MV>6,     abs(countryname_code month_code) vce(r)

matrix b=e(b)
matrix V=e(V)
matlist b
matlist V
scalar b1=b[1,1]
scalar b3=b[1,3]
scalar list b1 b3
scalar varb1=V[1,1]
scalar varb3=V[3,3]
scalar covb1b3=V[1,3]
scalar list b1 b3 varb1 varb3 covb1b3

sum hospital_beds_p1k if e(sample)
generate MVZ=((_n/1))
sum MVZ
replace MVZ=. if MVZ>13
sum MVZ

gen conbx=b1+b3*MVZ if MVZ!=.
gen consx=sqrt(varb1+varb3*(MVZ^2)+2*covb1b3*MVZ) if MVZ!=.
gen ax=1.96*consx
gen upperx=conbx+ax
gen lowerx=conbx-ax

gen yline=0

codebook hospital_beds_p1k

graph twoway hist hospital_beds_p1k, width(0.3) percent color(gs14) yaxis(2) ///
|| line conbx MVZ, clpattern(solid) clwidth(medium) clcolor(black) yaxis(1) ///
|| line upperx MVZ, clpattern(dash) clwidth(thin) clcolor(black) ///
|| line lowerx MVZ, clpattern(dash) clwidth(thin) clcolor(black) ///
|| line yline MVZ, clwidth(thin) clcolor(black) clpattern(solid) ///
|| , ///
xlabel(1 (1) 13, nogrid labsize(2)) ///
ylabel(-0.06 (0.02) 0.2, axis(1) nogrid labsize(2)) ///
ylabel(, axis(2) nogrid labsize(2)) ///
yscale(noline alt) ///
yscale(noline alt axis(2)) ///
xscale(noline) ///
legend(off) ///
xtitle("Hospital beds (per 1 000 inhabitants)" , size(2.5) ) ///
ytitle("Marginal Effect of Lagged Deaths" , axis(1) size(2.5)) ///
ytitle("Percentage of Observations" , axis(2) size(2.5)) ///
xsca(titlegap(2)) ///
ysca(titlegap(2)) ///
scheme(s2mono) graphregion(fcolor(white) ilcolor(white) lcolor(white)) note("Model: only with {&Delta}<0")  saving(a.gph, replace)

graph export "Figure3BL.eps", as(eps) name("Graph") preview(off) replace

graph export "Figure3BLpdf.pdf", as(pdf) name("Graph") replace

reghdfe stringencyindex c.l.windexinrow##c.hospital_beds_p1k l.stringencyindex  l.new_cases_per_million2 ///
 c.l.new_deaths_per_million2##c.hospital_beds_p1k if l.deltawindexinrow>=0  & MV>6,     abs(countryname_code month_code) vce(r)

matrix b=e(b)
matrix V=e(V)
matlist b
matlist V
scalar b1=b[1,1]
scalar b3=b[1,3]
scalar list b1 b3
scalar varb1=V[1,1]
scalar varb3=V[3,3]
scalar covb1b3=V[1,3]
scalar list b1 b3 varb1 varb3 covb1b3

drop conbx consx ax upperx lowerx 

gen conbx=b1+b3*MVZ if MVZ!=.
gen consx=sqrt(varb1+varb3*(MVZ^2)+2*covb1b3*MVZ) if MVZ!=.
gen ax=1.96*consx
gen upperx=conbx+ax
gen lowerx=conbx-ax

graph twoway hist hospital_beds_p1k, width(0.3) percent color(gs14) yaxis(2) ////
|| line conbx MVZ, clpattern(solid) clwidth(medium) clcolor(black) yaxis(1) ///
|| line upperx MVZ, clpattern(dash) clwidth(thin) clcolor(black) ///
|| line lowerx MVZ, clpattern(dash) clwidth(thin) clcolor(black) ///
|| line yline MVZ, clwidth(thin) clcolor(black) clpattern(solid) ///
|| , ///
xlabel(1 (1) 13, nogrid labsize(2)) ///
ylabel(-0.01 0 .01 .02 0.03 0.04, axis(1) nogrid labsize(2)) ///
ylabel(, axis(2) nogrid labsize(2)) ///
yscale(noline alt) ///
yscale(noline alt axis(2)) ///
xscale(noline) ///
legend(off) ///
xtitle("Hospital beds (per 1 000 inhabitants)" , size(2.5) ) ///
ytitle("Marginal Effect of W - Geographical Proximity" , axis(1) size(2.5)) ///
ytitle("Percentage of Observations" , axis(2) size(2.5)) ///
xsca(titlegap(2)) ///
ysca(titlegap(2)) ///
scheme(s2mono) graphregion(fcolor(white) ilcolor(white) lcolor(white)) note("Model: only with {&Delta} >=0") saving(b.gph, replace)
 
graph export "Figure3TL.eps", as(eps) name("Graph") preview(off) replace

graph export "Figure3TL.pdf", as(pdf) name("Graph") replace 
 
reghdfe stringencyindex c.l.windexinrow##c.hospital_beds_p1k l.stringencyindex  l.new_cases_per_million2 ///
 c.l.new_deaths_per_million2##c.hospital_beds_p1k if l.deltawindexinrow<0  & MV>6,     abs(countryname_code month_code) vce(r)

matrix b=e(b)
matrix V=e(V)
matlist b
matlist V
scalar b1=b[1,1]
scalar b3=b[1,3]
scalar list b1 b3
scalar varb1=V[1,1]
scalar varb3=V[3,3]
scalar covb1b3=V[1,3]
scalar list b1 b3 varb1 varb3 covb1b3

drop conbx consx ax upperx lowerx 

gen conbx=b1+b3*MVZ if MVZ!=.
gen consx=sqrt(varb1+varb3*(MVZ^2)+2*covb1b3*MVZ) if MVZ!=.
gen ax=1.96*consx
gen upperx=conbx+ax
gen lowerx=conbx-ax

graph twoway hist hospital_beds_p1k, width(0.3) percent color(gs14) yaxis(2) ////
|| line conbx MVZ, clpattern(solid) clwidth(medium) clcolor(black) yaxis(1) ///
|| line upperx MVZ, clpattern(dash) clwidth(thin) clcolor(black) ///
|| line lowerx MVZ, clpattern(dash) clwidth(thin) clcolor(black) ///
|| line yline MVZ, clwidth(thin) clcolor(black) clpattern(solid) ///
|| , ///
xlabel(1 (1) 13, nogrid labsize(2)) ///
ylabel(-0.04 (0.01) 0.04, axis(1) nogrid labsize(2)) ///
ylabel(, axis(2) nogrid labsize(2)) ///
yscale(noline alt) ///
yscale(noline alt axis(2)) ///
xscale(noline) ///
legend(off) ///
xtitle("Hospital beds (per 1 000 inhabitants)" , size(2.5) ) ///
ytitle("Marginal Effect of W - Geographical Proximity" , axis(1) size(2.5)) ///
ytitle("Percentage of Observations" , axis(2) size(2.5)) ///
xsca(titlegap(2)) ///
ysca(titlegap(2)) ///
scheme(s2mono) graphregion(fcolor(white) ilcolor(white) lcolor(white)) note("Model: only with delta<0")  saving(c.gph, replace)
 
graph export "Figure3TR.eps", as(eps) name("Graph") preview(off) replace

graph export "Figure3TR.pdf", as(pdf) name("Graph") replace 
 
gr combine b.gph c.gph a.gph, scheme(s2mono) graphregion(fcolor(white) ilcolor(white) lcolor(white))





